<template>
	<view class="selection">
		<view class="back">
			<image class="back-img" @click="back" src="../../../static/index/fanhui.png" mode=""></image>
			<view class="search">
				<input class="search-input" placeholder-style="color:#666666" type="text" value="" placeholder="牛奶" />
				<icon type="search" size="16" class="search-img" />
			</view>
		</view>

		<view class="content" style="height: 100vh;">
			<view class="left" id="left">
				<scroll-view :scroll-y="true" :scroll-with-animation="true" :scroll-into-view="clickToId"
					:style="{'height':windowHeight}">
					<view class="left-title">
						地区选择
					</view>
					<view class="left-selection" v-for="(item,index) in addressList" :key="index"
						@click="selectionPro(index,item)" :class="num==index?'leftActive':''" :id="'to'+index">
						{{item.name}}
					</view>
				</scroll-view>
			</view>
			<view class="right" id="right" v-show="isRight">
				<scroll-view :scroll-into-view="clickId" @scroll="scroll" :scroll-with-animation="true" :scroll-y="true"
					:style="{'height':windowHeight}">
					<view class="right-content">
						<view class="citys" v-for="(item,index) in cityList" :key="index" :id="'po'+index">
							<view class="citys-title" @click="clickCity(item,index)" >
								<text :class="cityNum==index?'cityActive':''">{{item.name}}</text>
							</view>
							<view class="areas">
								<view v-for="(item2,index2) in item.child" :key="index2" @click="clickArea(index2,index,item2)">
									<text :class="cityList[index].showFlag==index2?'textActive':''">{{item2.name}}</text>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import province from '@/service/province.js' //全国省
	import {getArea} from '@/service/user.js' //区域列表
	export default {
		data() {
			return {
				test:province,
				windowHeight: '0px',
				clickId: '',
				clickToId: '',
				currentNum: 0,
				topList: [],
				isLeftClick: false,
				isRight:false,//右边界面
				isShow:false,//区县
				earaId:'',//区县id
				cityList: [],
				addressList: [],
				num: 0, //左边选中效果   
				cityNum:0,//右边城市选择
				areaNum:0,//右边区县选择
				params:{
					pid:''
				}
			}
		},
		onLoad(options) {
			let _that = this;
			uni.getSystemInfo({
				success: function(res) {
					_that.windowHeight = res.windowHeight + 'px';
				}
			});
			_that.getNodesInfo();
			_that.addressList = _that.test.data.data//获取全省数据
		},
		methods: {
			// 返回
			back() {
				uni.navigateBack()
			},
			
			//点击右侧城市
			clickCity(item,index){   
				this.cityNum = index;
				// console.log(item.name,'右侧城市');
				this.$store.dispatch("city",item.name);
			},
			//右边区县选择
			clickArea(index2,index,item){
				this.cityList[index].showFlag = index2;
				// console.log(item.name,'右侧区县');
				this.$store.dispatch("eara",item.name);
				uni.navigateTo({
					url:'/myPackageA/index/agrotechny'
				})
			},
			//获取市区县
			getArea(){
				getArea(this.params).then(res=>{
					this.cityList = res.data
				})
			},
			//点击左侧菜单
			selectionPro(index,item) {
				this.params.pid = item.id;
				// console.log(item.name,'左侧省');
				this.$store.dispatch("province",item.name);
				this.num = index;
				this.clickId = "po" + index;
				this.isRight = true;
				this.isLeftClick=true;
				this.getArea();
			},
			scroll(e) {
				if (this.isLeftClick) {
					this.isLeftClick = false;
					return;
				}
				let scrollTop = e.target.scrollTop;
				for (let i = 0; i < this.topList.length; i++) {
					let h1 = this.topList[i];
					let h2 = this.topList[i + 1];
					if (scrollTop >= h1 && scrollTop < h2) {
						this.currentNum = i;
						this.clickToId = 'to' + i
					}
				}
			},
			//获取节点信息
			getNodesInfo() {
				const query = uni.createSelectorQuery().in(this);
				query.select('#right').boundingClientRect().exec((res) => {
					// console.log(res)
					// let nodes=res[0];
					let nodes = res
					let rel = [];
					nodes.map(item => {
						rel.push(item.top);
					})
					console.log(rel)
					this.topList = rel;
				});
			},
		}
	}
</script>

<style scoped lang="less">
	.content {
		padding-left: 40rpx;
		padding-right: 40rpx;
		padding-top: 176rpx;
		width: 100%;
		height: auto;
		display: flex;

		.right {
			margin-top: 54rpx;
			flex: 1;
			height: 100%;
			overflow: auto;
			position: relative;
			background-color: #FFFFFF;
			border-radius: 20rpx;

			.right-content {
				margin-bottom: 40rpx;

				.citys {
					margin-bottom: 20rpx;
					.citys-title{
						font-size: 36rpx;
						font-family: Source Han Sans CN;
						font-weight: 400;
						color: #333333;
					}
					.cityActive {
						font-size: 32rpx;
						font-family: Source Han Sans CN;
						font-weight: 400;
						color: #FF7200;
					}
					.areas {
						padding-left: 124rpx;
						display: flex;
						justify-content: flex-start;
						align-items: center;
						flex-wrap: wrap;

						text {
							font-size: 32rpx;
							font-family: Source Han Sans CN;
							font-weight: 400;
							color: #666666;
							margin-right: 20rpx;
						}

						.textActive {
							font-size: 32rpx;
							font-family: Source Han Sans CN;
							font-weight: 400;
							color: #FF7200;
						}
					}
				}

			}
		}

		.left {
			padding-top: 44rpx;
			width: 180rpx;
			justify-content: center;
			align-items: center;
			// height: 1000rpx;
			// overflow: scroll;
			height: 100%;
			overflow: auto;
			position: relative;

			.left-title {
				font-size: 36rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #333333;
				margin-bottom: 40rpx;
			}

			.left-selection {
				font-size: 32rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #333333;
				margin-bottom: 40rpx;
			}

			.leftActive {
				font-size: 36rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #FF7200;
			}
		}
	}

	.back {
		padding-top: 80rpx;
		width: 100%;
		height: 176rpx;
		background-color: #FF7200;
		display: flex;
		align-self: center;
		position: fixed;
		top: 0;
		left: 0;
	}

	.search {
		position: relative;
	}

	.search-input {
		background-color: #FFFFFF;
		border-radius: 30rpx;
		padding-left: 60rpx;
		width: 420rpx;
		height: 64rpx;
		z-index: 99;
	}

	.back-img {
		width: 40rpx;
		height: 50rpx;
		margin-top: 10rpx;
		margin-left: 40rpx;
	}

	.search-img {
		width: 32rpx;
		height: 32rpx;
		position: absolute;
		top: 16rpx;
		left: 18rpx;
	}
</style>
